package com.aimuceliang.aimuceliang.utils;

import android.util.Log;

import com.aimuceliang.aimuceliang.MyApp;
import com.aimuceliang.aimuceliang.green_dao.LongCheng;
import com.aimuceliang.aimuceliang.green_dao.LongChengDaoUtils;
import com.aimuceliang.aimuceliang.green_dao.TiChiCeLiang;
import com.aimuceliang.aimuceliang.green_dao.TiChiCeLiangDaoUtils;
import com.aimuceliang.aimuceliang.green_dao.WoCheng;
import com.aimuceliang.aimuceliang.green_dao.WoChengDaoUtils;

import java.io.File;
import java.io.IOException;
import java.util.List;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

/**
 * Created by ziabo on 2017/8/16.
 * 创建excel的工具类
 */

public class JXLUtils {

    /**
     * 创建笼称xls的方法
     */
    public static String createLongChengExcel(LongChengDaoUtils daoUtils) {
        WritableWorkbook workbook = null;
        String strExcelPath = getXLSPath("_longchengceliang_"+DateUtils.getCurrentFormatTime()+".xls");
        if (strExcelPath != null) {
            File file = new File(strExcelPath);
            try {
                workbook = Workbook.createWorkbook(file);
                WritableSheet ws = workbook.createSheet("笼秤测量", 0);
                List<LongCheng> longChengs = daoUtils.queryAllLongCheng();
                Label labelId = new Label(0, 0, "id");
                Label labelDeviceId = new Label(1, 0, "设备序号");
                Label labelTime = new Label(2, 0, "时间");
                Label labelEarId = new Label(3, 0, "耳标号");
                Label labelWeight = new Label(4, 0, "重量");
                Label labelOther = new Label(5, 0, "其他");

                ws.addCell(labelId);
                ws.addCell(labelDeviceId);
                ws.addCell(labelTime);
                ws.addCell(labelEarId);
                ws.addCell(labelWeight);
                ws.addCell(labelOther);

                for (int i = 0; i < longChengs.size(); i++) {
                    Label labelId_i = new Label(0, i + 1, longChengs.get(i).getId() + "");
                    Label labelDeviceId_i = new Label(1, i + 1, longChengs.get(i).getDeviceid() + "");
                    Label labelTime_i = new Label(2, i + 1, longChengs.get(i).getTime() + "");
                    Label labelEarId_i = new Label(3, i + 1, longChengs.get(i).getEarid() + "");
                    Label labelWeight_i = new Label(4, i + 1, longChengs.get(i).getWeight() + "");
                    Label labelOther_i = new Label(5, i + 1, longChengs.get(i).getOther() + "");

                    ws.addCell(labelId_i);
                    ws.addCell(labelDeviceId_i);
                    ws.addCell(labelTime_i);
                    ws.addCell(labelEarId_i);
                    ws.addCell(labelWeight_i);
                    ws.addCell(labelOther_i);
                }
                workbook.write();
                workbook.close();
            } catch (IOException | WriteException e) {
                e.printStackTrace();
                return null;
            }
        } else {
            Log.d("JXLUtils", "笼称文件创建失败");
            return null;
        }

        return strExcelPath;
    }

    /**
     * 创建窝称xls的方法
     */
    public static String createWoChengExcel(WoChengDaoUtils daoUtils) {
        WritableWorkbook workbook = null;
        String strExcelPath = getXLSPath("_wochengceliang_"+DateUtils.getCurrentFormatTime()+".xls");
        if (strExcelPath != null) {
            File file = new File(strExcelPath);
            try {
                workbook = Workbook.createWorkbook(file);
                WritableSheet ws = workbook.createSheet("窝秤测量", 0);
                List<WoCheng> woChengs = daoUtils.queryAllWoCheng();
                Label labelId = new Label(0, 0, "id");
                Label labelDeviceId = new Label(1, 0, "设备序号");
                Label labelTime = new Label(2, 0, "时间");
                Label labelEarId = new Label(3, 0, "耳标号");
                Label labelNumber = new Label(4, 0, "头数");
                Label labelWeight = new Label(5, 0, "总重量");
                Label labelWeightMin = new Label(6, 0, "最轻");
                Label labelWeightMax = new Label(7, 0, "最重");
                Label labelWeightAvarage = new Label(8, 0, "平均重");
                Label labelOther = new Label(9, 0, "其他");

                ws.addCell(labelId);
                ws.addCell(labelDeviceId);
                ws.addCell(labelTime);
                ws.addCell(labelEarId);
                ws.addCell(labelNumber);
                ws.addCell(labelWeight);
                ws.addCell(labelWeightMin);
                ws.addCell(labelWeightMax);
                ws.addCell(labelWeightAvarage);
                ws.addCell(labelOther);

                for (int i = 0; i < woChengs.size(); i++) {
                    Label labelId_i = new Label(0, i + 1, woChengs.get(i).getId() + "");
                    Label labelDeviceId_i = new Label(1, i + 1, woChengs.get(i).getDeviceid() + "");
                    Label labelTime_i = new Label(2, i + 1, woChengs.get(i).getTime() + "");
                    Label labelEarId_i = new Label(3, i + 1, woChengs.get(i).getEarid() + "");
                    Label labelNumber_i = new Label(4, i + 1, woChengs.get(i).getNumber() + "");
                    Label labelWeight_i = new Label(5, i + 1, woChengs.get(i).getWeight() + "");
                    Label labelWeightMin_i = new Label(6, i + 1, woChengs.get(i).getWeightmin() + "");
                    Label labelWeightMax_i = new Label(7, i + 1, woChengs.get(i).getWeightmax() + "");
                    Label labelWeightAva_i = new Label(8, i + 1, woChengs.get(i).getWeightavarage() + "");
                    Label labelOther_i = new Label(9, i + 1, woChengs.get(i).getOther() + "");

                    ws.addCell(labelId_i);
                    ws.addCell(labelDeviceId_i);
                    ws.addCell(labelTime_i);
                    ws.addCell(labelEarId_i);
                    ws.addCell(labelNumber_i);
                    ws.addCell(labelWeight_i);
                    ws.addCell(labelWeightMin_i);
                    ws.addCell(labelWeightMax_i);
                    ws.addCell(labelWeightAva_i);
                    ws.addCell(labelOther_i);
                }
                workbook.write();
                workbook.close();
            } catch (IOException | WriteException e) {
                e.printStackTrace();
                return null;
            }
        } else {
            Log.d("JXLUtils", "窝称文件创建失败");
            return null;
        }

        return strExcelPath;
    }

    /**
     * 创建体秤xls的方法
     */
    public static String createTiChiCeLiangExcel(TiChiCeLiangDaoUtils daoUtils) {
        WritableWorkbook workbook = null;
        String strExcelPath = getXLSPath("_tichiceliang_"+DateUtils.getCurrentFormatTime()+".xls");
        if (strExcelPath != null) {
            File file = new File(strExcelPath);
            try {
                workbook = Workbook.createWorkbook(file);
                WritableSheet ws = workbook.createSheet("体尺测量", 0);
                List<TiChiCeLiang> tccls = daoUtils.queryAllTiChiCeLiang();
                Label labelId = new Label(0, 0, "id");
                Label labelDeviceId = new Label(1, 0, "设备序号");
                Label labelTime = new Label(2, 0, "采集时间");
                Label labelEarId = new Label(3, 0, "耳标号");
                Label labelWeight = new Label(4, 0, "重量");
                Label labelHeight = new Label(5, 0, "体高");
                Label labelTiXieChang = new Label(6, 0, "体斜长");
                Label labelLength = new Label(7, 0, "体宽");
                Label labelXiongWei = new Label(8, 0, "胸围");
                Label labelChannelId = new Label(9, 0, "通道号");
                Label labelGuanWei = new Label(10, 0, "管围");
                Label labelXiongShen = new Label(11, 0, "胸深");
                Label labelWeiKuan = new Label(12, 0, "围宽");
                Label labelWeiChang = new Label(13, 0, "围长");
                Label labelTenLength = new Label(14, 0, "十字部宽");
                Label labelOther = new Label(15, 0, "其他");

                ws.addCell(labelId);
                ws.addCell(labelDeviceId);
                ws.addCell(labelTime);
                ws.addCell(labelEarId);
                ws.addCell(labelWeight);
                ws.addCell(labelHeight);
                ws.addCell(labelTiXieChang);
                ws.addCell(labelLength);
                ws.addCell(labelXiongWei);
                ws.addCell(labelChannelId);
                ws.addCell(labelGuanWei);
                ws.addCell(labelXiongShen);
                ws.addCell(labelWeiKuan);
                ws.addCell(labelWeiChang);
                ws.addCell(labelTenLength);
                ws.addCell(labelOther);

                for (int i = 0; i < tccls.size(); i++) {
                    Label labelId_i = new Label(0, i + 1, tccls.get(i).getId() + "");
                    Label labelDeviceId_i = new Label(1, i + 1, tccls.get(i).getDeviceid() + "");
                    Label labelTime_i = new Label(2, i + 1, tccls.get(i).getTime() + "");
                    Label labelEarId_i = new Label(3, i + 1, tccls.get(i).getEarid() + "");
                    Label labelWeight_i = new Label(4, i + 1, tccls.get(i).getWeight() + "");
                    Label labelHeight_i = new Label(5, i + 1, tccls.get(i).getHeight() + "");
                    Label labelTiXieChang_i = new Label(6, i + 1, tccls.get(i).getTixiechang() + "");
                    Label labelLength_i = new Label(7, i + 1, tccls.get(i).getLength() + "");
                    Label labelXiongWei_i = new Label(8, i + 1, tccls.get(i).getXiongwei() + "");
                    Label labelChannelId_i = new Label(9, i + 1, tccls.get(i).getChannelid() + "");
                    Label labelGuanWei_i = new Label(10, i + 1, tccls.get(i).getGuanwei() + "");
                    Label labelXiongShen_i = new Label(11, i + 1, tccls.get(i).getXiongshen() + "");
                    Label labelWeiKuan_i = new Label(12, i + 1, tccls.get(i).getWeikuan() + "");
                    Label labelWeiChang_i = new Label(13, i + 1, tccls.get(i).getWeichang() + "");
                    Label labelTenLength_i = new Label(14, i + 1, tccls.get(i).getTenlength() + "");
                    Label labelOther_i = new Label(15, i + 1, tccls.get(i).getOther() + "");

                    ws.addCell(labelId_i);
                    ws.addCell(labelDeviceId_i);
                    ws.addCell(labelTime_i);
                    ws.addCell(labelEarId_i);
                    ws.addCell(labelWeight_i);
                    ws.addCell(labelHeight_i);
                    ws.addCell(labelTiXieChang_i);
                    ws.addCell(labelLength_i);
                    ws.addCell(labelXiongWei_i);
                    ws.addCell(labelChannelId_i);
                    ws.addCell(labelGuanWei_i);
                    ws.addCell(labelXiongShen_i);
                    ws.addCell(labelWeiKuan_i);
                    ws.addCell(labelWeiChang_i);
                    ws.addCell(labelTenLength_i);
                    ws.addCell(labelOther_i);
                }
                workbook.write();
                workbook.close();
            } catch (IOException | WriteException e) {
                e.printStackTrace();
                return null;
            }
        } else {
            Log.d("JXLUtils", "体尺测量文件创建失败");
            return null;
        }

        return strExcelPath;
    }

    /**
     * 获取excel文件夹path的方法
     */
    public static String getXLSPath(String fileName) {
        String path = MyApp.getInstance().getCacheDir() + File.separator + "amor_excel"
                + File.separator + AppUtils.getDeviceId(MyApp.getInstance()) + fileName;
        File file = new File(path);
        if (file.exists()) {
            return path;
        } else {
            file.getParentFile().mkdirs();
            try {
                boolean newFile = file.createNewFile();
                if (newFile) {
                    return path;
                } else {
                    return null;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
